﻿import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;

public class Tester {
	//private static boolean DEBUG = true;
	private static int iPort = 6789;
	private static String strIP = "127.0.0.1";
	//private static JTextField txtSchleife;
	private static JTextField txtDatum;
	private static JTextField txtStichwort;
	private static JTextField txtZusatzInfo;
	private static JTextField txtStrasse;
	private static JTextField txtStrasse2;
	private static JTextField txtOrt;
	private static JTextField txtZusatzOrt;
	private static JTextField txtIP;
	private static JTextField txtPort;
	private static JTabbedPane jTabP1 = new JTabbedPane();
	private static JPanel jP1 = new JPanel();
	private static JPanel jP2 = new JPanel();
	private static String strMessage = "xxx";
	public JFrame frame;
	private JTextField txtObjekt;
	private JComboBox comboBox = new JComboBox();

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Tester window = new Tester();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Tester() {
		initialize();
		
	}
	/**
	 * @param args
	 */
	public void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 400);
		frame.setTitle("AlarmTester");
		frame.setResizable(false);
		frame.getContentPane().setLayout(new BorderLayout());

		JLabel lblAlarmschleife = new JLabel("Alarmschleife");
		lblAlarmschleife.setBounds(10, 66, 90, 14);
		jP1.add(lblAlarmschleife);
		
		JLabel lblDatum = new JLabel("Uhrzeit Datum");
		lblDatum.setBounds(10, 91, 90, 14);
		jP1.add(lblDatum);
		
		JLabel lblStichwort = new JLabel("Stichwort");
		lblStichwort.setBounds(10, 116, 90, 14);
		jP1.add(lblStichwort);
		
		JLabel lblZusatzInfo = new JLabel("Zusatz Info");
		lblZusatzInfo.setBounds(10, 168, 90, 14);
		jP1.add(lblZusatzInfo);
		
		JLabel lblStrasse = new JLabel("Strasse/Nr");
		lblStrasse.setBounds(10, 193, 90, 14);
		jP1.add(lblStrasse);
		
		JLabel lblStrasse_1 = new JLabel("Strasse 2");
		lblStrasse_1.setBounds(10, 218, 46, 14);
		jP1.add(lblStrasse_1);
		
		JLabel lblOrt = new JLabel("Ort");
		lblOrt.setBounds(10, 243, 46, 14);
		jP1.add(lblOrt);
		
		JLabel lblZusatz = new JLabel("Zusatz");
		lblZusatz.setBounds(10, 268, 46, 14);
		jP1.add(lblZusatz);
		
//		txtSchleife = new JTextField();
//		txtSchleife.setText("Einsatzalarm 894");
//		txtSchleife.setBounds(130, 35, 136, 20);
//		jf.getContentPane().add(txtSchleife);
//		txtSchleife.setColumns(10);
		
		txtDatum = new JTextField();
		txtDatum.setText("20:00 01.01.2013");
		txtDatum.setBounds(130, 88, 136, 20);
		jP1.add(txtDatum);
		txtDatum.setColumns(10);
		
		JButton btnSenden = new JButton("Senden");
		btnSenden.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				strMessage = comboBox.getSelectedItem() + ": " + txtDatum.getText() + "*" + txtStichwort.getText() + "*" + txtZusatzInfo.getText() + "*" + txtObjekt.getText() + "*" + txtStrasse.getText() + "*" + txtStrasse2.getText() + "*" + txtOrt.getText() + "*" + txtZusatzOrt.getText() + "*** ** *";
				TCPClient(strMessage);
			}
		});
		btnSenden.setBounds(130, 293, 89, 23);
		jP1.add(btnSenden);
		
		txtStichwort = new JTextField();
		txtStichwort.setText("THU1");
		txtStichwort.setBounds(130, 113, 40, 20);
		jP1.add(txtStichwort);
		txtStichwort.setColumns(10);
		
		txtZusatzInfo = new JTextField();
		txtZusatzInfo.setText("droht zu springen pers");
		txtZusatzInfo.setBounds(130, 165, 272, 20);
		jP1.add(txtZusatzInfo);
		txtZusatzInfo.setColumns(10);
		
		txtStrasse = new JTextField();
		txtStrasse.setText("Albert-Schweitzer-Stra\u00DFe 4");
		txtStrasse.setBounds(130, 190, 272, 20);
		jP1.add(txtStrasse);
		txtStrasse.setColumns(10);
		
		txtStrasse2 = new JTextField();
		txtStrasse2.setText("Albert-Schweitzer-Stra\u00DFe");
		txtStrasse2.setBounds(130, 215, 272, 20);
		jP1.add(txtStrasse2);
		txtStrasse2.setColumns(10);
		
		txtOrt = new JTextField();
		txtOrt.setText("21423 Winsen_WL");
		txtOrt.setBounds(130, 240, 272, 20);
		jP1.add(txtOrt);
		txtOrt.setColumns(10);
		
		txtZusatzOrt = new JTextField();
		txtZusatzOrt.setText("äöüß");
		txtZusatzOrt.setBounds(130, 265, 272, 20);
		jP1.add(txtZusatzOrt);
		txtZusatzOrt.setColumns(10);
		
		JLabel lblIpadresse = new JLabel("IP-Adresse");
		lblIpadresse.setBounds(10, 41, 90, 14);
		jP1.add(lblIpadresse);
		
		txtIP = new JTextField();
		txtIP.setText("127.0.0.1");
		txtIP.setBounds(130, 38, 86, 20);
		jP1.add(txtIP);
		txtIP.setColumns(10);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(235, 41, 31, 14);
		jP1.add(lblPort);
		
		txtPort = new JTextField();
		txtPort.setText("6789");
		txtPort.setBounds(264, 38, 40, 20);
		jP1.add(txtPort);
		txtPort.setColumns(10);
		
		//JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Einsatzalarm 892", "Einsatzalarm 893", "Einsatzalarm 894", "Einzelfahrzeug"}));
		comboBox.setBounds(130, 63, 136, 20);
		jP1.add(comboBox);
		
		JLabel lblObjekt = new JLabel("Objekt");
		lblObjekt.setBounds(10, 141, 46, 14);
		jP1.add(lblObjekt);
		
		txtObjekt = new JTextField();
		txtObjekt.setText("Hochhaus Albert-Schweitzer-Stra\u00DFe");
		txtObjekt.setBounds(130, 138, 272, 20);
		jP1.add(txtObjekt);
		txtObjekt.setColumns(10);
	
		//System.out.println(textPort.getText());
		
		strIP = txtIP.getText();
		iPort = Integer.parseInt(txtPort.getText());

		frame.getContentPane().add(jTabP1, BorderLayout.CENTER);
		jP1.setLayout(null);
		jP2.setLayout(null);
		jTabP1.addTab("Anzeige", jP1);
		
		JLabel lblTestAnzeige = new JLabel("Test -> Anzeige");
		lblTestAnzeige.setBounds(170, 0, 90, 14);
		jP1.add(lblTestAnzeige);
		jTabP1.addTab("ComListener", jP2);
		
		JLabel lblTestComlistener = new JLabel("Test -> ComListener");
		lblTestComlistener.setBounds(157, 0, 103, 14);
		jP2.add(lblTestComlistener);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		// System.out.println(formater.format(cal.getTime()));
//		if (args.length > 0) {
//			if (DEBUG)
//				System.out.println("DEBUG>> Parameter IP: " + args[0]);
//			strIP = args[0];
//			if (args.length > 1) {
//				if (DEBUG)
//					System.out.println("DEBUG>> Parameter Message: " + args[1]);
//			}
//		}
//		if (DEBUG)
//			System.out.println("DEBUG>> main finished");
//		new Tester(args[1]);
	}

//	/**
//	 * 
//	 */
//	public Tester(String a) {
//		if (DEBUG)
//			System.out.println("DEBUG>> Konstruktor: " + a);
//		TCPClient(a);
//	}

	/**
	 * Nachricht an Server senden
	 */
	private static void TCPClient(String message) {

		try {
			Socket s = new Socket(strIP, iPort);
			//DataOutputStream out = new DataOutputStream(s.getOutputStream());
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"));
			//BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));

			out.write(message + '\n');
			// while ((str = in.readLine()) != null) {
			// if (DEBUG) System.out.println("DEBUG>> [EMPFANGEN4]: " +
			// formater.format(cal.getTime()) + " --- " + str);
			// }
			out.close();
			//in.close();
			s.close();
		} catch (IOException err) {
			System.err.println("Error: " + err);
			System.exit(0);
		}
	}
}
